package com.senyint.imsdk;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesKit {
	private Properties prop = null ;
	private String file = null ;
	
	public PropertiesKit() {
		prop = new Properties();
		file = System.getProperty("user.dir")+"cfg.properties"; 
		System.out.println(file+"..................");
	}
	
	public PropertiesKit(String file) {
		prop = new Properties();
		this.file = file ;
	}
	
	public void load() throws IOException{
		load(this.file);
	}
	
	public void load(String file) throws IOException{
		if(file == null)
			return ;
		
//		Properties temp = new Properties();
		FileInputStream in = new FileInputStream(file);
		prop.load(in);
		in.close();
//		Iterator<Object> it= temp.keySet().iterator();  
//		while(it.hasNext()){  
//            Object key = it.next();  
//            Object val = temp.get(key);  
//            System.out.println(key+":::"+val);  
//            prop.setProperty(key.toString(), val.toString());
//        }  
//		temp.clear();
	}

	public String getString(String key) {
		return getString(getSystemString(key),null);
	}
	
	public String getString(String key,String def) {
		String val = getSystemString(key);
		if(val != null){
			return val ;
		}
		return prop.getProperty(key,def);
	}
	
	public String getSystemString(String key) {
		return System.getProperty(key);
	}
	
	public void put(String key ,Object val){
		if(val == null) return ;
		prop.put(key, String.valueOf(val));
	}
	
	public void remove(String key) {
		prop.remove(key);
	}
	
	public void save(){
        try {  
            FileOutputStream outputFile = new FileOutputStream(file);  
            prop.store(outputFile, "auto saved");// property类关键的store方法  
            outputFile.close();  
            // propertie.list(System.out);  
            System.out.println("File was saved!");  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException ioe) {  
            ioe.printStackTrace();  
        }  
	}

}

